﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace uacars
{
    public partial class frmImprimir : Form
    {
        string nombre;
        DataGridViewPrinter dgvImprimirPrinter;

        public frmImprimir()
        {
            InitializeComponent();
        }

        public frmImprimir(DataGridView tabla, string formulario)
        {
            InitializeComponent();
            dgvImprimir.DataSource = tabla.DataSource;
            if (formulario == "CLIENTES")
            {
                dgvImprimir.Columns[12].Visible = false;
                dgvImprimir.Columns[13].Visible = false;
            }
            if (formulario == "EMPLEADOS")
            {
                dgvImprimir.Columns[14].Visible = false;
                dgvImprimir.Columns[15].Visible = false;
            }
            nombre = formulario;
        }

        private void buttonSalir_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonImprimir_Click(object sender, EventArgs e)
        {
            if (Imprimir())
                printDocument1.Print();
        }

        private void buttonVista_Click(object sender, EventArgs e)
        {
            PrintDialog MyPrintDialog = new PrintDialog();

            printDocument1.DocumentName = "REPORTE " + nombre;
            printDocument1.PrinterSettings = MyPrintDialog.PrinterSettings;
            printDocument1.DefaultPageSettings = MyPrintDialog.PrinterSettings.DefaultPageSettings;
            printDocument1.DefaultPageSettings.Margins = new Margins(10, 10, 10, 10);
            printDocument1.DefaultPageSettings.Landscape = true;


            if (MessageBox.Show("Desea ver el reporte centrado en la pagina", "Información", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                dgvImprimirPrinter = new DataGridViewPrinter(dgvImprimir, printDocument1, true, true, nombre, new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point), Color.Black, true);
            else
                dgvImprimirPrinter = new DataGridViewPrinter(dgvImprimir, printDocument1, false, true, nombre, new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point), Color.Black, true);

            PrintPreviewDialog MyPrintPreviewDialog = new PrintPreviewDialog();
            MyPrintPreviewDialog.Document = printDocument1;
            MyPrintPreviewDialog.ShowDialog();
        }

        private bool Imprimir()
        {
            PrintDialog MyPrintDialog = new PrintDialog();
            MyPrintDialog.AllowCurrentPage = false;
            MyPrintDialog.AllowPrintToFile = false;
            MyPrintDialog.AllowSelection = false;
            MyPrintDialog.AllowSomePages = false;
            MyPrintDialog.PrintToFile = false;
            MyPrintDialog.ShowHelp = false;
            MyPrintDialog.ShowNetwork = false;

            if (MyPrintDialog.ShowDialog() != DialogResult.OK)
                return false;

            printDocument1.DocumentName = "REPORTE " + nombre;
            printDocument1.PrinterSettings = MyPrintDialog.PrinterSettings;
            printDocument1.DefaultPageSettings = MyPrintDialog.PrinterSettings.DefaultPageSettings;
            printDocument1.DefaultPageSettings.Margins = new Margins(10, 10, 10, 10);
            printDocument1.DefaultPageSettings.Landscape = true;

            return true;
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            bool mas = dgvImprimirPrinter.DrawDataGridView(e.Graphics);
            if (mas == true)
                e.HasMorePages = true;
        }
    }
}
